package com.itheima.mall.web.pms;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.commons.result.Result;
import com.itheima.mall.dubbo.api.pms.PmsProductAttributeCategoryService;
import com.itheima.mall.dubbo.api.pms.PmsProductAttributeService;
import com.itheima.mall.entity.domain.pms.ProductAttribute;
import com.itheima.mall.entity.domain.pms.ProductAttributeCategory;
import com.itheima.mall.entity.dto.ProductAttributeCategoryDto;
import com.itheima.mall.entity.page.MyPage;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {
    @DubboReference
    private PmsProductAttributeCategoryService productAttributeCategoryService;
    @DubboReference
    private PmsProductAttributeService attributeService;
    //添加商品属性分类
    @PostMapping("create")
    public Result create(@RequestBody String name) throws UnsupportedEncodingException {
        String[] split = name.split("=");
        ProductAttributeCategory productAttributeCategory =new ProductAttributeCategory();
        String decode = URLDecoder.decode(split[1], "UTF-8");
        productAttributeCategory.setName(decode);
        productAttributeCategoryService.save(productAttributeCategory);
        return Result.success("添加成功");
    }
    //删除单个商品属性分类
    @GetMapping("/delete/{id}")
    public Result delete(Integer id){
        productAttributeCategoryService.removeById(id);
        return Result.success("删除成功");
    }
    //分页获取所有属性分类
    @GetMapping("list")
    public Result list(Integer pageNum,Integer pageSize){
       MyPage<ProductAttributeCategory> page =  productAttributeCategoryService.getPage(pageNum,pageSize);
        return Result.success(page);
    }
    //获取所有商品属性分类及其下属性
    @GetMapping("/list/withAttr")
    public Result listWithAttr(){
        ProductAttributeCategoryDto attributeCategoryDto = new ProductAttributeCategoryDto();
        List<ProductAttributeCategory> list = productAttributeCategoryService.list();
        for (ProductAttributeCategory productAttributeCategory : list) {
            Long id = productAttributeCategory.getId();
           List<ProductAttribute> attributeList =  attributeService.getId(id);
            BeanUtils.copyProperties(productAttributeCategory,attributeCategoryDto);
            attributeCategoryDto.setAttributeList(attributeList);
        }
        return Result.success(attributeCategoryDto);
    }
    //修改商品属性分类
    @PostMapping("/update/{id}")
    public Result update(@RequestBody String name,@PathVariable Long id){
        ProductAttributeCategory categoryServiceById = productAttributeCategoryService.getById(id);
        categoryServiceById.setName(name);
        productAttributeCategoryService.updateById(categoryServiceById);
        return Result.success("修改成功");
    }
    //获取单个商品属性分类信息
    @GetMapping("/{id}")
    public Result getById(Long id){
        ProductAttributeCategory productAttributeCategory = productAttributeCategoryService.getById(id);
        return Result.success(productAttributeCategory);
    }
}
